
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="设备型号:" prop="equipmentModel">
    <el-input v-model="formData.equipmentModel" :clearable="true" placeholder="请输入设备型号" />
</el-form-item>
        <el-form-item label="设备名称:" prop="deviceName">
    <el-input v-model="formData.deviceName" :clearable="true" placeholder="请输入设备名称" />
</el-form-item>
        <el-form-item label="资料类型（1-设备彩页 2-设备画册 3-用户手册 4-安装手册 5-配置文件 6-演示视频 7-常见问题）:" prop="dataType">
    <el-select v-model="formData.dataType" placeholder="请选择资料类型（1-设备彩页 2-设备画册 3-用户手册 4-安装手册 5-配置文件 6-演示视频 7-常见问题）" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in dataTypeOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="资料格式（1-pdf 2-zip/rar 3-apk 4-html 5-dxf 6-stp 7-mp4）:" prop="dataFormat">
    <el-select v-model="formData.dataFormat" placeholder="请选择资料格式（1-pdf 2-zip/rar 3-apk 4-html 5-dxf 6-stp 7-mp4）" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in dataFormatOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="资料标题:" prop="dataTitle">
    <el-input v-model="formData.dataTitle" :clearable="true" placeholder="请输入资料标题" />
</el-form-item>
        <el-form-item label="文件大小:" prop="fileSize">
    <el-input-number v-model="formData.fileSize" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="更新日期:" prop="updateDate">
    <el-date-picker v-model="formData.updateDate" type="date" style="width:100%" placeholder="选择日期" :clearable="true" />
</el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createInformation,
  updateInformation,
  findInformation
} from '@/api/device/information'

defineOptions({
    name: 'InformationForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const dataTypeOptions = ref([])
const dataFormatOptions = ref([])
const formData = ref({
            equipmentModel: '',
            deviceName: '',
            dataType: '',
            dataFormat: '',
            dataTitle: '',
            fileSize: 0,
            updateDate: new Date(),
        })
// 验证规则
const rule = reactive({
               dataType : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               dataFormat : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findInformation({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    dataTypeOptions.value = await getDictFunc('dataType')
    dataFormatOptions.value = await getDictFunc('dataFormat')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createInformation(formData.value)
               break
             case 'update':
               res = await updateInformation(formData.value)
               break
             default:
               res = await createInformation(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
